<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width = device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable = 0">
	
<title>注册</title>

<link href="../../public/css/base.css" rel="stylesheet">
<link href="../../public/css/login.css" rel="stylesheet">
<script src="../../public/js/jquery.min.2.1.4.js"></script>
<script src="../../public/js/md5.js"></script>
<script src="../../public/js/sort_ascii.js"></script>
</head>

<body>
<header class="header">
	<div class="header-return">
	    <a href="javascript:history.go(-1);"></a>
	</div>
	
	<div class="logo">注册</div>
</header>

<section class="container">
	<div class="register">
		<div class="form-widget">
			<form id="register_form" onsubmit="return false" action="##" method="post">
				<div class="form-box">
					<div class="form-group">
						<label class="form-label">手机号码</label>
						<div class="form-control form-phone">
							<span>+86</span>
							<input placeholder="您的手机号码" name="mobile">
						</div>
					</div>

					<div class="form-group">
						<label class="form-label">手机验证码</label>
						<div class="form-control form-getcode">
							<input placeholder="填写验证码"  name="m_yzm">
							<button onclick="mobile_yanz(this)">获取验证码</button>
						</div>
					</div>

					<div class="form-group">
						<label class="form-label">昵&nbsp;&nbsp;称</label>

						<div class="form-control">
							<input placeholder="请输入昵称" name="nickname">
						</div>
					</div>

					<div class="form-group">
						<label class="form-label">登录密码</label>

						<div class="form-control">
							<input type="password" placeholder="请输入字母开头6~32位字母加数字" name="password">
						</div>
					</div>

					<div class="form-group">
						<label class="form-label">再次输入密码</label>
						<div class="form-control">
							<input type="password" placeholder="请输入字母开头6~32位字母加数字" name="qpassword">
						</div>
					</div>

					
					<div class="form-group">
						<label class="form-label">邀请人UID</label>

						<div class="form-control">
							<input placeholder="邀请人UID" name="agent_user">
						</div>
					</div>
					

					

				</div>

				<div class="form-submit">
					<button type="submit" class="form-submit-btn" onclick="register_insert()">确定</button>
				</div>
			</form>
		</div>
	</div>
</section>
<script type="text/javascript">
	function mobile_yanz(e) {
			var mobile = $("input[name=mobile]").val();
			if(mobile == ''){
			    alert('手机号码不能为空！');
			    return false;
			}
			var time = Date.parse(new Date())/1000;
		    let mobile_yanz_datas = {mobile,time};
		    let mobile_yanz_data = sort_ascii(mobile_yanz_datas);
			var mobile_yanz_code = sort_code(mobile_yanz_data);
            $.ajax({
                type: "POST",//方法
                url: url+"/index.php/api/User/user_find_yzm" ,//表单接收url
                data: {mobile:mobile,code:mobile_yanz_code,time:time},
                success: function(msg) {
                  //提交成功的提示词或者其他反馈代码
                    alert(msg.msg);
                    setTime($(e));
                },
                error : function() {
                  //提交失败的提示词或者其他反馈代码
					alert('error！');
					//setTime($(e));	//仅供测试，正式使用需删除
                }
            });
        }
	
	function setTime(e) {
	    var dom = e
        var count = 30;
        dom.attr('disabled', true),dom.css({color: '#333',}),dom.text(count + '秒'),count--;
        const countDown = setInterval(() => {
            if (count === 0) {
                dom.text('重新获取').removeAttr('disabled');
                dom.css({color: '#f00'});
                clearInterval(countDown);
            } else {
                dom.attr('disabled', true);
                dom.css({color: '#333',});
                dom.text(count + '秒');
            }
            count--;
        }, 1000);
    }

	function register_insert() {
			var mobile = $("input[name=mobile]").val();
			var password = $("input[name=password]").val();
			var qpassword = $("input[name=qpassword]").val();
			var yzm = $("input[name=m_yzm]").val();
			var nickname = $("input[name=nickname]").val();
			var agent_user = $("input[name=agent_user]").val();
			var time = Date.parse(new Date())/1000;
			
		    let register_datas = {mobile,password,qpassword,yzm,nickname,time,agent_user};
		    let register_data = sort_ascii(register_datas);
			var register_code = sort_code(register_data);
            $.ajax({
                type: "POST",//方法
                url: url+"/index.php/api/User/register" ,//表单接收url
                data: {mobile:mobile,password:password,qpassword:qpassword,yzm:yzm,nickname:nickname,code:register_code,time:time,agent_user:agent_user},
                success: function(data) {
                  //提交成功的提示词或者其他反馈代码
					if(data.code == 1){
						alert(data.msg);
						location.href="../login/login.html";
					}

					if(data.code == 0){
						alert(data.msg);
					}
                },
                error : function() {
                  //提交失败的提示词或者其他反馈代码
                    alert('失败！');
                }
            });
        }

</script>
</body>
</html>
